package com.epoint.mobileoa.tools;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import com.epoint.mobileoa.R;



import android.content.Context;

/**
 * @author liyc
 * @time 2012-2-8 上午9:33:40
 * @annotation 获取配置文件
 */
public class PropertiesHelp {

	public static String getProperty(Context con,String key){
		Map<String, String> propertyMap = new HashMap<String, String>();   
		String value = propertyMap.get(key);   
		Properties property = new Properties();   
		InputStream inputFile = null;   
		if (value == null) {   
			// 实例化inputFile,如config.properties文件的位置    
			inputFile = con.getResources().openRawResource(R.raw.mobileoa);

			// 装载配置文件    
			try {
				property.load(inputFile);
			} catch (IOException e) {
				e.printStackTrace();
			}finally{
				try {
					if(inputFile!=null){
						inputFile.close();
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			value = property.getProperty(key).trim();   
			propertyMap.put(key, value);   
		}   
		return value;
	} 

	public static String getProperty(String key){
		Map<String, String> propertyMap = new HashMap<String, String>();   
		String value = propertyMap.get(key);   
		Properties property = new Properties();   
		InputStream inputFile = null;   
		if (value == null) {   
			// 装载配置文件    
			try {
				// 实例化inputFile,如config.properties文件的位置    
				File f = new File("../src/setting.properties");
				FileInputStream fis = new FileInputStream(f);
				inputFile = fis;
				property.load(inputFile);
			} catch (IOException e) {
				e.printStackTrace();
			}finally{
				try {
					if(inputFile!=null){
						inputFile.close();
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			value = property.getProperty(key).trim();   
			propertyMap.put(key, value);   
		}   
		return value;
	} 

	public Map<String,String> getProperty(Context con,List<String> propertyList){   
		// 定义Map用于存放结果    
		Map<String,String> propertyMap = new HashMap<String,String>();   
		Properties property = new Properties();   
		InputStream inputFile = null;   
		// 实例化inputFile    
		inputFile = con.getResources().openRawResource(R.raw.mobileoa);
		// 装载配置文件    
		try {
			property.load(inputFile);
		} catch (IOException e) {
			e.printStackTrace();
		} finally{
			try {
				if(inputFile!=null){
					inputFile.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		for (String name : propertyList) {   
			// 从配置文件中获取属性存入map中    
			String data = property.getProperty(name);   
			propertyMap.put(name, data);   
		}   

		return propertyMap;   
	}   
}
